home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapter2 / gettype.c < prev    next >
C/C++ Source or Header  |  1996-04-27  |  8KB  |  247 lines

  1.  
  2. #include <windows.h>  
  3. #include "gettype.h"  
  4. #include "sqlext.h"
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "SQLGetTypeInfo()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.     WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.    
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107.  
  108. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  109. {
  110. static HENV hEnv  = NULL;
  111. static HDBC hDBC  = NULL;
  112. static HWND hList = NULL;
  113.  
  114.    switch( uMsg )
  115.    {
  116.       case WM_CREATE  :
  117.               {
  118.                  RETCODE retCode;
  119.  
  120.                  // Allocate Environment and Connection.
  121.                  //.....................................
  122.                  SQLAllocEnv( &hEnv );
  123.                  SQLAllocConnect( hEnv, &hDBC );
  124.  
  125.                  // Connect to data source.
  126.                  //........................
  127.                  retCode = SQLConnect( hDBC, "Test Data Source", SQL_NTS, NULL, 0, NULL, 0 );
  128.  
  129.                  if ( retCode != SQL_SUCCESS )
  130.                  {
  131.                     SQLFreeConnect( hDBC );
  132.                     SQLFreeEnv( hEnv );
  133.                     MessageBox( hWnd, "Could not connect to 'Test Data Source'", 
  134.                                 NULL, MB_OK | MB_ICONSTOP );
  135.                     return( -1 );
  136.                  }
  137.  
  138.                  if ( retCode == SQL_SUCCESS )
  139.  
  140.                  hList = CreateWindow( "LISTBOX", "",    
  141.                                     WS_CHILD | LBS_STANDARD | 
  142.                                     WS_VISIBLE | LBS_NOINTEGRALHEIGHT, 
  143.                                     0, 0, 
  144.                                     0, 0,  
  145.                                     hWnd,              
  146.                                     (HMENU)101,              
  147.                                     hInst,         
  148.                                     NULL               
  149.                                   );
  150.               }
  151.               break;
  152.  
  153.       case WM_SIZE :
  154.               MoveWindow( hList, 0, 0, LOWORD( lParam ), HIWORD( lParam ), TRUE );
  155.               break; 
  156.               
  157.       case WM_COMMAND :
  158.               switch( LOWORD( wParam ) )
  159.               {
  160.                  case IDM_TEST :
  161.                         {
  162.                            HSTMT   hStmt;
  163.                            UCHAR   szTypeName[ 51 ];
  164.                            SDWORD  dwTypeLen;
  165.                            RETCODE retCode;
  166.  
  167.                            SQLAllocStmt( hDBC, &hStmt );
  168.                            
  169.                            // Retrieve all the data type names.
  170.                            //..................................
  171.                            retCode = SQLGetTypeInfo( hStmt, SQL_ALL_TYPES ); 
  172.  
  173.                            if ( retCode == SQL_SUCCESS )
  174.                            {
  175.                               // Bind a column to the TYPE_NAME in the
  176.                               // result set generated from SQLGetTypeInfo().
  177.                               //............................................
  178.                               SQLBindCol( hStmt, 1, SQL_C_CHAR, szTypeName, 
  179.                                           sizeof( szTypeName ), &dwTypeLen );
  180.  
  181.                               // Fetch all the records and
  182.                               // put them in the list box.
  183.                               //..........................
  184.                               retCode = SQLFetch( hStmt );
  185.  
  186.                               while ( retCode == SQL_SUCCESS )
  187.                               {
  188.                                  SendMessage( hList, LB_ADDSTRING, 0, (LPARAM)szTypeName );
  189.  
  190.                                  retCode = SQLFetch( hStmt );
  191.                               }
  192.                            }
  193.  
  194.                            SQLFreeStmt( hStmt, SQL_DROP );
  195.                         }
  196.                         break;
  197.  
  198.                  case IDM_ABOUT :
  199.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  200.                         break;
  201.  
  202.                  case IDM_EXIT :
  203.                         DestroyWindow( hWnd );
  204.                         break;
  205.               }
  206.               break;
  207.       
  208.       case WM_DESTROY :
  209.               PostQuitMessage(0);
  210.               SQLDisconnect( hDBC );
  211.               SQLFreeConnect( hDBC );
  212.               SQLFreeEnv( hEnv );
  213.               break;
  214.  
  215.       default :
  216.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  217.    }
  218.  
  219.    return( 0L );
  220. }
  221.  
  222.  
  223. LRESULT CALLBACK About( HWND hDlg,           
  224.                         UINT message,        
  225.                         WPARAM wParam,       
  226.                         LPARAM lParam)
  227. {
  228.    static  HFONT hfontDlg;
  229.  
  230.    switch (message) 
  231.    {
  232.        case WM_INITDIALOG: 
  233.                return (TRUE);
  234.  
  235.        case WM_COMMAND:                              
  236.                if (   LOWORD(wParam) == IDOK         
  237.                    || LOWORD(wParam) == IDCANCEL)    
  238.                {
  239.                        EndDialog(hDlg, TRUE);        
  240.                        return (TRUE);
  241.                }
  242.                break;
  243.    }
  244.  
  245.    return (FALSE); 
  246. }
  247.